package hehe.core.base;

import hehe.container.Container;
import hehe.router.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * app web 应用流程
 *<B>说明：</B>
 *<pre>
 * 每个Servlet都由此类处理
 *</pre>
 *<B>示例：</B>
 *<pre>
 *  略
 *</pre>
 *<B>日志：</B>
 *<pre>
 *  略
 *</pre>
 *<B>注意事项：</B>
 *<pre>
 *  略
 *</pre>
 */
abstract public class Application extends HttpServlet
{
    protected long starttime;

    public Application()
    {

    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
        this.starttime =  System.currentTimeMillis();
        this.processRequest(req,resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.processRequest(req,resp);
    }


    /**
     * 统一处理HttpServletRequest 入口
     *<B>说明：</B>
     *<pre>
     * get,post 等请求方法的处理入口
     *</pre>
     * @param req HttpServletRequest
     * @param resp HttpServletResponse
     */
    public void processRequest(HttpServletRequest req, HttpServletResponse resp)
    {
        try {
            this.run(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建控制器对象
     *<B>说明：</B>
     *<pre>
     * 略
     *</pre>
     * @param controllerName 控制器名称
     * @return 控制器对象
     */
    protected Object createController(String controllerName)
    {

        try {
            Class<?> controllerClazz = Class.forName(controllerName);

            return controllerClazz.newInstance();
        } catch (ClassNotFoundException ex) {
            return null;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 调用controller action 方法
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     * @param controller 控制器对象
     * @param actionName action 方法名称
     * @param request　Servlet　请求
     * @return 字符串或Response对象
     */
    protected Object runAction(Object controller ,String actionName,HttpServletRequest request)
    {
        Class cls = controller.getClass();
        try {
            Class[] argsClass = new Class[]{HttpServletRequest.class};
            Object[] args = {request};
            if (controller instanceof BaseController) {
                // 调用初始化方法
                Method initMethod = cls.getMethod("init",argsClass);
                initMethod.invoke(controller, args);
                // 调用对应action
                Method actionMethod = cls.getMethod(actionName);
                return actionMethod.invoke(controller);
            } else {
                Method method = cls.getMethod(actionName,argsClass);
                return method.invoke(controller, args);
            }

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 执行业务逻辑入口
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    protected abstract void run(HttpServletRequest req, HttpServletResponse resp)
            throws Exception;




}
